/*
	分页表格工具类
*/
var UI={
	"getPageTable":function(table,url,cols,elem="#think-table",height="600"){
		table.render({
			elem: elem
			,height:height
			,url: url //数据接口
			,page: {layout:['count', 'prev', 'page', 'next'	, 'refresh', 'skip']}//开启分页
			,limit:15

			,parseData:function(res){
				return {
					"code":0,
					"msg":res['msg'],
					"count":res['data']['total'],
					"data":res['data']['data']
				};
			}
			,cols: [cols]

		});
	},
	"openLayer":function(url,x=750,y=600,btn=['确定', '取消'],confirmButton="#submit",endCallback = 1){
		layer.open({
			type: 2,
			anim: 2,
			area: [x+'px', y+'px'],
			btn: btn,
			yes: function(index, layero){
				//点击确认触发 iframe 内容中的按钮提交
				let submit = layero.find('iframe').contents().find(confirmButton);
				submit.click();
			},
			content: url,//这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
			end:function(){
				console.log('close layer')
				// think-table
				if(endCallback != 1){
					endCallback();
				}
			}
		});
	},
	//查看订单详情
	"orderInfo":function(url){
		layer.open({
			type: 2,
			title: false,
			shadeClose: true,
			shade: 0.01,
			closeBtn: 0,
			anim: 3,
			resize:false,
			isOutAnim: false,
			offset: 'r',
			area: ['500px', '100%'],
			content: url //iframe的url
		});
	},
	"delete":function(id,callback,url,str = '确认删除吗？'){
		layer.confirm(str, {
			btn: ['确认','取消'] //可以无限个按钮
		}, function(index, layero){
			//按钮【按钮一】的回调
			Api.del(id,function(res){
				layer.msg(res['msg']);
				if(res['code']===200){
					layer.close(index);
				}
				callback(res);
			},url);

		}, function(index){
			//按钮【按钮二】的回调
		});
	},
	//更新锁
	"lock_update":function(id,callback,obj){
		layer.confirm('确认更新吗？', {
			btn: ['确认','取消'] //可以无限个按钮
		}, function(index, layero){
			//按钮【按钮一】的回调
			Api.lock_update(id,function(res){
				console.log(res);
				layer.msg(res['msg']);
				if(res['code']===200){
					layer.close(index);
				}
				callback(res);
			});

		}, function(index){
			//按钮【按钮二】的回调
		});
	},
	"feedback_del":function(url,id,callback,obj){
		layer.confirm('确认删除吗？', {
			btn: ['确认','取消'] //可以无限个按钮
		}, function(index, layero){
			//按钮【按钮一】的回调
			return HttpUtils.get(url,{"id":id},function(res){
				layer.msg(res['msg']);
				if(res['code']===200){
					layer.close(index);
				}
				callback(res);
			});

		}, function(index){
			//按钮【按钮二】的回调
		});
	},

	"depositBack":function(url,id,callback,obj){
		layer.confirm('确认退款吗？', {
			btn: ['确认','取消'] //可以无限个按钮
		}, function(index, layero){
			//按钮【按钮一】的回调
			return HttpUtils.get(url,{"o_id":id},function(res){
				layer.msg(res['msg']);
				if(res['code']===200){
					layer.close(index);
				}
				callback(res);
			});

		}, function(index){
			//按钮【按钮二】的回调
		});
	},

	//切换房间清扫状态
	"houseClear":function(param,callback){
		Api.houseClear('houseClear',param,function(res){
			layer.msg(res['msg']);
			if(res['code']===200){
			}
			callback(res);
		})
	},

	//单个图片上传
    "upload":function (id,callback,accept='file') {
        layui.use('upload', function() {
            let $ = layui.jquery
                , upload = layui.upload;

            let uploadInst = upload.render({
                accept:'file',
                elem: id
                , url: '/admin/file_up?accept='+accept //改成您自己的上传接口

                , done: function (res) {
                    //如果上传失败
                    callback(res)
                    //上传成功
                }
            });
        })
    },

    "local":function (id,callback,accept='file') {
        layui.use('upload', function() {
            let $ = layui.jquery
                , upload = layui.upload;

            let uploadInst = upload.render({
                accept:'file',
                elem: id
                , url: '/admin/file_up2?accept='+accept //改成您自己的上传接口

                , done: function (res) {
                    //如果上传失败
                    callback(res)
                    //上传成功
                }
            });
        })
    },
	"uploads":function (id,callback,beforeCallback = 0) {
		layui.use('upload', function() {
            let $ = layui.jquery
				, upload = layui.upload;
				upload.render({
					elem: '#test2'
					,url: 'https://upload-z2.qiniup.com/' //改成您自己的上传接口
					,multiple: true
					,before: function(obj){
						//预读本地文件示例，不支持ie8
						if (beforeCallback !== 0){
							beforeCallback(obj);
						}
					}
					,data:{
                        token:function () {
                            let token;
                            $.ajax({
                                async: false,
                                url:'/admin/getUploadToken',
                                success: function (res) {
                                    token = res.data.token;
                                }
                            })
                            return token;
                        },
                        key:function () {
                            return Date.parse(new  Date());
                        }
                    }
                    ,done: function(res){
						//上传完毕
						callback(res);
					}
				});
		});
	},
	//上传安装包文件
	"uploadFiles":function(id,callback,url='upload'){

		layui.use('upload', function() {
            let $ = layui.jquery
				, upload = layui.upload;

			//普通图片上传
            let uploadInst = upload.render({
				elem: id
				, url: url //改成您自己的上传接口
				,exts:'apk'
				, done: function (res) {
					//如果上传失败
					callback(res)
					//上传成功
				}
				,progress: function(n, elem){
					console.log(n);
					var percent = n + '%' //获取进度百分比
					element.progress('demo', percent); //可配合 layui 进度条元素使用
					//以下系 layui 2.5.6 新增
					console.log(elem); //得到当前触发的元素 DOM 对象。可通过该元素定义的属性值匹配到对应的进度条。
				}
			});
		})
	}
};
